<?php

abstract class AbstractSociableController extends AppController
{
	var $uses = array();
	var $components = array('Session','RequestHandler','Auth');
	protected $facebookModel = 'Sociable.FacebookAccount';
	protected $twitterModel = 'Sociable.TwitterAccount';
	protected $linkedinModel = 'Sociable.LinkedinAccount';
	var $helpers = array('Sociable.SocialConnect');
	var $entity = null;
/**
	 * Facebook data receiver function and stores all the necessary info to FacebookAccounts table.
	 * @return unknown_type
	 */
	
	function beforeFilter()
	{
		$this->Auth->allow(array('xd_reciver'));
		parent::beforeFilter();
		$this->setEntity();
	}
	function xd_reciver()
	{	//Configure::write('debug',2);
		if(isset($_GET['session']))
		{
			$this->saveFacebookSession();
		}
		$this->layout = false;
	}
	
	function saveFacebookSession()
	{
		$this->getAccountModel($this->facebookModel)
		->saveFbSession($this->entity);
		if($this->params['action'] == 'saveFacebookSession')
		{
			if( $this->RequestHandler->isAjax() == true)
			{
				$this->_stop();
			}else
			{
				$this->redirect($this->referer());
			}
		}	
	}
	
	protected function getAccountModel($model)
	{
		return cri($model);
	}
	
	/**
	 * Initiates authentication of twitter
	 * @return unknown_type
	 */
	function linkTwitterAccount()
	{
		if($this->getAccountModel($this->twitterModel)->hasAccount($this->entity))
		{
			$this->redirect('/');
		}
		$this->Session->write('afterTwitterAuth', $this->referer());
		$authenticationUrl = $this->getAccountModel($this->twitterModel)
		->callApi('getAuthenticateUrl');
		$this->redirect($authenticationUrl);
	}
	
	/**
	 * Twitter posts info to this page once user has authorized the app
	 * @return unknown_type
	 */
	function twitterAuth()
	{
		$redirect = '/';
		if(isset($_GET['oauth_token']))
		{
			$API = $this->getAccountModel($this->twitterModel)->getApi();
			$API->setToken($_GET['oauth_token']);
			$token = false;
			try
			{
				$token = $API->getAccessToken();  
			}
			catch (Exception $e)
			{
				$token = false;
			}
			if($token !==false)
			{
				$this->saveTwitterAuth($token);
				$this->successMsg(__('Your twitter account has been linked successfully!'));
			}else
			{
				
			}
			$redirect = $this->Session->read('afterTwitterAuth');
			$this->Session->delete('afterTwitterAuth');
		}
		$this->redirect($redirect);
	}
	/**
	 * 
	 */
	protected function saveTwitterAuth($token)
	{
		$this->getAccountModel($this->twitterModel)->saveNewAccount($token,$this->entity);
	}
	/**
	 * 
	 */
	function unlinkFbAccount()
	{
		$this->getAccountModel($this->facebookModel)->unlinkAccount($this->entity);
		$this->Session->write(__('Your facebook account has been unlinked.'));
		$this->redirect($this->referer());
	}
	/**
	 * 
	 */
	function unlinkTwitterAccount()
	{
		$this->getAccountModel($this->twitterModel)->unlinkAccount($this->entity);
		$this->Session->write(__('Your facebook account has been unlinked.'));
		$this->redirect($this->referer());
	}
	
	/**
	 *	Requests Linkedin for auth token and
	 *	Redirect for authentication
	 */
	function addLinkedinAccount()
	{
		if($this->getAccountModel($this->linkedinModel)->hasAccount($this->entity))
		{
			$this->redirect('/');
		}
		$this->Session->write('afterLinkedinAuth.referer', $this->referer());
		$authenticateUrl = $this->getAccountModel($this->linkedinModel)
		->callApi('getAuthenticateUrl'); 
		$this->redirect($authenticateUrl);
	}
	
	function linkedinAuth()
	{
		$redirect = '/';
		if(isset($_GET['oauth_token']))
		{
			$API = $this->getAccountModel($this->linkedinModel)->getApi();
			$token = false;
			try
			{
				$token = $API->getAccessToken($_GET['oauth_token']);  
			}
			catch (Exception $e)
			{
				$token = false;
			}
			if($token !==false)
			{
				$this->saveLinkedinAuth($token);
				$this->successMsg(__('Your linkedin account has been linked successfully!'));
			}else
			{
				
			}
			$redirect = $this->Session->read('afterLinkedinAuth.referer');
			$this->Session->delete('afterLinkedinAuth');
		}
		$this->redirect($redirect);
	}
	/**
	 * 
	 */
	protected function saveLinkedinAuth($token)
	{
		$this->getAccountModel($this->linkedinModel)->saveNewAccount($token ,$this->entity);
	}
	
	/**
	 * 
	 */
	protected function setEntity()
	{
		if($this->Session->check('Auth.User.id'))
		{
			$this->entity = $this->Session->read('Auth.User.id');
		}
	}
	
	
}
?>